package DEEPERsource.DEEPERsource.source.machinelearning.features;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import treedata.types.PennPOS;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import deeper.Interaction;
import edu.uci.ics.jung.graph.Vertex;
import graph.Keys;

/**
 * Feature builder that takes into account POS in parse path. 
 * @author tfayruzo
 *
 */
public class POSFeatureBuilder implements FeatureBuilder {
	
	private List<Attribute> structure;
	private Instances data;
		
	public POSFeatureBuilder(boolean verbs){
		structure = new ArrayList<Attribute>();
		for(PennPOS pos : verbs?PennPOS.verbs():PennPOS.values())
			structure.add(new Attribute(pos.name()));
		FastVector  v = new FastVector(); 
		for(Attribute a : structure)
			v.addElement(a);
		data = new Instances("",v,1);
	}

	/**
	 * Returns a numeric feature vector, that counts POS
	 * that occur in shortest parse path  
	 * @param i
	 * @return
	 */
	public Instance getFeatureVector(Interaction i) {
		Instance inst = new Instance(structure.size());
		Set<String> posSet = new HashSet<String>();
		for(Vertex v : i.depPath.vertices){
			posSet.add((String)v.getUserDatum(Keys.POS));
		}
		for(Enumeration<Attribute> e = data.enumerateAttributes(); e.hasMoreElements();){
			Attribute a = e.nextElement(); 
			if(posSet.contains(a.name()))
				inst.setValue(a, new Double(1));
			else
				inst.setValue(a, new Double(0));
		}
		return inst;
	}

	public Instances getInstanceStructure() {		
		return data;
	}

}
